#region File header
// Project: SharpWars.Core, File: LoadGameForm.cs
// Namespace: SharpWars.Core.Forms, Class: LoadGameForm
// Path: F:\Coder\Projekty\Windows\Gry\SharpWars\SharpWars.Core\Forms, Author: Sebastian Haba (sho@gisz.net.pl)
// Code lines: 156, Size of file: 3,93 KB
// Creation date: 2007-02-25 16:33
// Last modified: 2007-02-25 18:24
#endregion
#region Using directives
using SharpWars.Core.Network;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
#endregion

namespace SharpWars.Core.Forms
{
	public partial class LoadGameForm : Form
	{
		private Client client = null;

		private GameRoomForm grf = null;

		public LoadGameForm()
		{
			InitializeComponent();
		}

		/// <summary>
		/// Create load game form
		/// </summary>
		/// <param name="client">Client</param>
		public LoadGameForm( Client client, GameRoomForm grf ) : this()
		{
			this.grf = grf;
			this.client = client;
		}

		private void OnLoad( object sender, EventArgs e )
		{
			string[] saveFiles = Directory.GetFiles( Application.StartupPath + "\\SavedGames\\" );
			foreach( string save in saveFiles )
			{
				if( IsSaveFileOk( save ) )
				{
					this.savedGamesList.Items.Add( Path.GetFileNameWithoutExtension( save ) );
				} // if (IsSaveFileOk)
			} // foreach (save)
		}

		/// <summary>
		/// Is save file ok
		/// </summary>
		/// <param name="path">Path</param>
		/// <returns>Bool</returns>
		private bool IsSaveFileOk( string path )
		{
			try
			{
				FileStream fs = new FileStream( path, FileMode.Open, FileAccess.Read, FileShare.None );
				BinaryReader br = new BinaryReader( fs );

				if( br.ReadString() == "SWS" )
				{
					int playersCount = br.ReadInt32();
					if( playersCount == grf.ConnectedPlayers.Count )
					{
						br.ReadString();

						for( int i = 0; i < playersCount; i++ )
						{
							br.ReadInt32();
							if( grf.ConnectedPlayers[i].Name != br.ReadString() )
							{
								return false;
							} // if (grf.ConnectedPlayers[i].Name)
						} // for (int)
					} // if (playersCount)
					else
					{
						return false;
					} // else
				}// if (br.ReadString)
				else
				{
					return false;
				} // else

				br.Close();
				fs.Close();
				fs.Dispose();

				return true;
			} // try
			catch
			{
				return false;
			} // catch
		} // IsSaveFileOk(path)

		/// <summary>
		/// Get save details
		/// </summary>
		/// <param name="playersCount">Players count</param>
		/// <param name="date">Date</param>
		private void GetSaveDetails( string path, out int playersCount, out string date )
		{
			try
			{
				FileStream fs = new FileStream( path, FileMode.Open, FileAccess.Read, FileShare.None );
				BinaryReader br = new BinaryReader( fs );

				if( br.ReadString() == "SWS" )
				{
					playersCount = br.ReadInt32();
					date = br.ReadString();
				} // if (br.ReadString)
				else
				{
					playersCount = 0;
					date = null;
				} // else

				br.Close();
				fs.Close();
				fs.Dispose();
			} // try
			catch
			{
				playersCount = 0;
				date = null;
			} // catch
		} // GetSaveDetails(playersCount, date)

		private void OnLoadClick( object sender, EventArgs e )
		{
			if( this.savedGamesList.Items.Count > 0 )
			{
				this.client.LoadGame( (string)this.savedGamesList.Items[this.savedGamesList.SelectedIndex] );
			} // if (this.savedGamesList.Items.Count)
		}

		/// <summary>
		/// On selected index changed
		/// </summary>
		/// <param name="sender">Sender</param>
		/// <param name="e">E</param>
		private void OnSelectedIndexChanged( object sender, EventArgs e )
		{
			int playersCount;
			string date;

			string path = Application.StartupPath + "\\SavedGames\\" + this.savedGamesList.Items[this.savedGamesList.SelectedIndex] + ".save";
			GetSaveDetails( path, out playersCount, out date );

			if( date != null && date != string.Empty )
			{
				this.numberOfPlayers.Text = playersCount.ToString();
				this.created.Text = date;
			} // if (date)
		} // OnSelectedIndexChanged(sender, e)
	}
}